﻿<%@ WebHandler Language="C#" Class="SoultionHandler" %>

using System.Data;
using System.Text;
using Dln.Common;
using Dln.Common.Helper;
using System.Collections.Generic;
using Dln.Model;
using System;
using System.Web;
using Dln.Handler;
using Dos.ORM;

public class SoultionHandler : HandlerBase
{
    public void GetWorkSoultionTable()
    {
        var param = CommonHelper.GetDataGridParam(_httpRequestForm);
        Dictionary<string, string> queryDic = CommonHelper.StringToDictionary(param.Query);
        StringBuilder strwhere = new StringBuilder();
        strwhere.Append(" DelStatus=0 ");
        if (queryDic != null && queryDic.Count > 0)
        {
            if (queryDic.ContainsKey("txtStartDate"))
            {
                strwhere.AppendFormat(" and AddTime>'{0}' ", queryDic["txtStartDate"].Trim());
            }
            if (queryDic.ContainsKey("txtEndDate"))
            {
                strwhere.AppendFormat(" and AddTime<'{0}' ", queryDic["txtEndDate"].Trim() + " 23:59:59");
            }

            if (queryDic.ContainsKey("txtKeyWords"))
            {
                strwhere.AppendFormat(" and (Title like '%{0}%' or Describe like '%{0}%' or Solution like '%{0}%' or EmployeeName like '%{0}%') ", queryDic["txtKeyWords"].Trim());
            }
        }
        PagingInfo pageInfo = new PagingInfo()
        {
            TableName = "WorkSoultionType",
            Fields = "*",
            PrimaryKey = "Id",
            Where = strwhere.ToString(),
            Order = param.SortName + " " + param.SortOrder,
            PageSize = param.RP,
            PageIndex = param.Page
        };
        int totalCount;
        DataTable dt = SqlHelper.GetPageTable(out totalCount, pageInfo);
        var jquerygridobject = new JqueryGridObject
        {
            DataGridParam = param,
            total = totalCount,
            QueryDataTable = dt
        };
        string jsonText = JSONHelper.JqueryGridObjectToJson(jquerygridobject);
        Context.Response.Write(jsonText);
    }

    /// <summary>
    /// 发布问题
    /// </summary>
    public void AddArticle()
    {
        int strArticleType = Convert.ToInt32(_httpRequestForm["selectArticleType"]);
        string strArticleTitle = _httpRequestForm["txtArticleTitle"].ToString();
        string strArticleContent = _httpRequestForm["txtArticleContent"].ToString();
        string strArticleContent1 = _httpRequestForm["txtArticleContent1"].ToString();

        var workSoultionModel = new WorkSoultion()
        {
            Title = strArticleTitle,
            Type = strArticleType,
            Describe = strArticleContent,
            Solution = strArticleContent1,
            AddTime = DateTime.Now,
        };
        HttpCookie userCookie = HttpContext.Current.Request.Cookies["LoginInfo"];
        if (userCookie != null && userCookie["UserName"] != null)
        {
            var uptModel = SqlHelper.Context.From<Employee>().Where(d => d.Phone == userCookie["UserName"]).ToFirstDefault();
            if (uptModel != null)
            {
                workSoultionModel.EmployeeName = uptModel.EmployeeName;
            }
        }

        var count1 = SqlHelper.Context.Insert<WorkSoultion>(workSoultionModel);
        if (count1 > 0)
        {
            this.Context.Response.Write("{\"state\":\"" + "success" + "\",\"info\":\"" + "发布成功！" + "\",\"ArticleId\":\"" + count1 + "\"}");
            return;
        }

        PrintErrorJson("发布失败！");
    }

    /// <summary>
    /// 修改问题
    /// </summary>
    public void EditArticle()
    {
        int IDPlus = Convert.ToInt32(_httpReuqestString["IDPlus"]);
        int strArticleType = Convert.ToInt32(_httpRequestForm["selectArticleType"]);
        string strArticleTitle = _httpRequestForm["txtArticleTitle"].ToString();
        string strArticleContent = _httpRequestForm["txtArticleContent"].ToString();
        string strArticleContent1 = _httpRequestForm["txtArticleContent1"].ToString();

        var uptModel = SqlHelper.Context.From<WorkSoultion>().Where(d => d.Id == IDPlus).ToFirstDefault();
        if (uptModel == null)
        {
            PrintErrorJson("不存在要修改的数据");
            return;
        }

        uptModel.Title = strArticleTitle;
        uptModel.Type = strArticleType;
        uptModel.Describe = strArticleContent;
        uptModel.Solution = strArticleContent1;

        var count1 = SqlHelper.Context.Update<WorkSoultion>(uptModel);
        if (count1 > 0)
        {
            this.Context.Response.Write("{\"state\":\"" + "success" + "\",\"info\":\"" + "修改成功！" + "\",\"ArticleId\":\"" + IDPlus + "\"}");
            return;
        }

        PrintErrorJson("修改失败！");
    }

    /// <summary>
    /// 删除问题
    /// </summary>
    public void DeleteArticle()
    {

        int IDPlus = Convert.ToInt32(_httpReuqestString["IDPlus"]);

        var uptModel = SqlHelper.Context.From<WorkSoultion>().Where(d => d.Id == IDPlus).First();
        if (uptModel == null)
        {
            PrintErrorJson("不存在要删除的数据");
            return;
        }

        var count = SqlHelper.Context.Delete<WorkSoultion>(IDPlus);
        if (count > 0)
        {
            PrintSuccessJson("删除成功！");
            return;
        }

        PrintErrorJson("删除失败！");
    }


    /// <summary>
    /// 注册
    /// </summary>
    public void Register()
    {
        string name = _httpRequestForm["name"].ToString();
        string phone = _httpRequestForm["phone"].ToString();
        string password = _httpRequestForm["password"].ToString();

        var uptModel = SqlHelper.Context.From<Employee>().Where(d => d.Phone == phone).ToList();
        if (uptModel != null && uptModel.Count > 0)
        {
            PrintErrorJson("系统已存在该手机号！");
            return;
        }

        var employeeModel = new Employee()
        {
            Phone = phone,
            EmployeeName = name,
            LoginPassword = password,
            AddTime = DateTime.Now
        };
        var count1 = SqlHelper.Context.Insert<Employee>(employeeModel);
        if (count1 > 0)
        {
            PrintSuccessJson("注册成功");
        }

    }

    /// <summary>
    /// 安全退出
    /// </summary>
    public void loginOutSystem()
    {
        HttpCookie userCookie = this.Context.Request.Cookies["LoginInfo"];
        if (userCookie != null)
        {
            this.Context.Response.Cookies.Remove("LoginInfo");
        }
        PrintSuccessJson("退出成功！");
    }


    /// <summary>
    /// 拉取微信用户
    /// </summary>
    public void GetSubscribe()
    {
        var result = WeChatSdk.GetUsersList();
        foreach (var item in result)
        {
            var UserInfoModel = WeChatSdk.GetUserInfo(item.ToString());

            if (UserInfoModel != null)
            {
                var uptModel = SqlHelper.Context.From<WeChatUsers>().Where(d => d.Openid == UserInfoModel.openid).ToFirst();
                if (uptModel == null)
                {
                    var weChatUsersModel = new WeChatUsers();
                    weChatUsersModel.AddTime = DateTime.Now;
                    weChatUsersModel.Openid = UserInfoModel.openid;
                    weChatUsersModel.Sex = UserInfoModel.sex;
                    weChatUsersModel.Nickname = UserInfoModel.nickname;
                    weChatUsersModel.Headimgurl = UserInfoModel.headimgurl;
                    var count1 = SqlHelper.Context.Insert<WeChatUsers>(weChatUsersModel);
                }
                else
                {
                    uptModel.Nickname = UserInfoModel.nickname;
                    uptModel.Headimgurl = UserInfoModel.headimgurl;
                    var count1 = SqlHelper.Context.Update<WeChatUsers>(uptModel);
                }
            }

        }
    }

    /// <summary>
    /// 微信用户列表
    /// </summary>
    public void GetWeChatTable()
    {
        var param = CommonHelper.GetDataGridParam(_httpRequestForm);
        Dictionary<string, string> queryDic = CommonHelper.StringToDictionary(param.Query);
        StringBuilder strwhere = new StringBuilder();
        strwhere.Append(" DelStatus=0 ");
        if (queryDic != null && queryDic.Count > 0)
        {
            if (queryDic.ContainsKey("txtKeyWords"))
            {
                strwhere.AppendFormat(" and Nickname like '%{0}%' or CardID like '%{0}%' ", queryDic["txtKeyWords"].Trim());
            }
        }
        PagingInfo pageInfo = new PagingInfo()
        {
            TableName = "WeChatUsers",
            Fields = "*",
            PrimaryKey = "Id",
            Where = strwhere.ToString(),
            Order = param.SortName + " " + param.SortOrder,
            PageSize = param.RP,
            PageIndex = param.Page
        };
        int totalCount;
        DataTable dt = SqlHelper.GetPageTable(out totalCount, pageInfo);
        var model = SqlHelper.Context.From<PositionTypes>().ToList();
        Dictionary<String, String> dic = new Dictionary<string, string>();
        if (model != null && model.Count > 0)
        {
            foreach (var a in model)
            {
                dic.Add(a.Id.ToString(), a.TypeName);
            }
        }
        dt = SqlHelper.DictionaryGetTable(dt, "PositionRoles", dic);
        var jquerygridobject = new JqueryGridObject
        {
            DataGridParam = param,
            total = totalCount,
            QueryDataTable = dt
        };
        string jsonText = JSONHelper.JqueryGridObjectToJson(jquerygridobject);
        Context.Response.Write(jsonText);
    }

    /// <summary>
    /// 
    /// </summary>
    public void EditWeChatInfo()
    {
        int iSSendWeChat1 = Convert.ToInt32(_httpRequestForm["txtEditName"]);
        int type = string.IsNullOrEmpty(_httpRequestForm["selectProductCategory"]) ? 0 : Convert.ToInt32(_httpRequestForm["selectProductCategory"]);
        string category = _httpRequestForm["txtEditCardId"];
        if (!string.IsNullOrEmpty(category))
        {
            var model = SqlHelper.Context.From<WeChatUsers>().Where(d => d.CardID == category).ToList();
            if (model.Count > 0)
            {
                PrintErrorJson("系统已存在该工号，请确认工号");
                return;
            }
        }


        int IDPlus = Convert.ToInt32(_httpReuqestString["IDPlus"]);
        var weChatUsers = SqlHelper.Context.From<WeChatUsers>().Where(d => d.Id == IDPlus).ToFirstDefault();
        if (weChatUsers == null)
        {
            PrintErrorJson("不存在要修改的数据");
            return;
        }

        weChatUsers.AddTime = DateTime.Now;
        weChatUsers.ISSendWeChat1 = iSSendWeChat1;
        weChatUsers.PositionRoles = type;
        weChatUsers.CardID = category;

        var count1 = SqlHelper.Context.Update<WeChatUsers>(weChatUsers);
        if (count1 > 0)
        {
            PrintSuccessJson("修改成功！");
            return;
        }
        PrintErrorJson("修改失败！");
    }


}